package com.laikan.reader.core.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.reader.R;
import com.socks.library.KLog;

/**
 * 自定义顶部标题栏
 *
 * @author Administrator
 */
public class CommonTitleLayout extends RelativeLayout {

    private final int ANDROID_BACKGROUND = 1;
    private TypedArray ta;
    private String title;
    private ImageView iv_back;
    private TextView tv_title;
    private CommonTitleListener mCommonTitleListener;
    private RelativeLayout rl_title;
    private int bg;

    public CommonTitleLayout(Context context) {
        super(context);
    }

    public CommonTitleLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CommonTitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 1.加载布局
        LayoutInflater.from(context).inflate(R.layout.common_title, this);

        // 2.获取自定义属性的值
        initAttrs(context, attrs);

        // 3.获取自定义布局的控件
        initView();

        // 4.初始化自定义布局中控件的事件
        setListener();

        // 5.初始化自定义布局中控件的值
        initData();

    }

    /**
     * 初始化数据
     *
     * @date 2015-12-15 下午3:18:15
     * @author yjh
     */
    private void initData() {
        tv_title.setText(title);
        rl_title.setBackgroundResource(bg);
    }

    /**
     * 设置自定义布局控件的事件
     *
     * @date 2015-12-15 下午3:17:45
     * @author yjh
     */
    private void setListener() {
        iv_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mCommonTitleListener != null) {
                    mCommonTitleListener.onBack();
                }
                ((Activity)getContext()).finish();
            }
        });
    }

    /**
     * 获取自定义布局的控件
     *
     * @date 2015-12-15 下午3:17:15
     * @author yjh
     */
    private void initView() {
        rl_title = (RelativeLayout)findViewById(R.id.laikan_title);
        iv_back = (ImageView)findViewById(R.id.iv_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
    }

    /**
     * 获取自定义属性的值
     *
     * @param context
     * @param attrs
     *
     * @date 2015-12-15 下午3:16:07
     * @author yjh
     */
    public void initAttrs(Context context, AttributeSet attrs) {
        ta = context.obtainStyledAttributes(attrs, R.styleable.common_title);
        title = ta.getString(R.styleable.common_title_title);
        bg = ta.getResourceId(ANDROID_BACKGROUND, 0);
        KLog.e("my_title_title = " + title);
        ta.recycle();
    }

    public void setCommonTitileListener(CommonTitleListener l) {
        mCommonTitleListener = l;
    }

    /**
     * 设置标题
     *
     * @param title 标题名称
     *
     * @date 2015-12-15 下午5:51:13
     * @author yjh
     */
    public void setTitle(String title) {
        tv_title.setText(title);
    }

    /**
     * 设置返回键的背景颜色
     *
     * @param resid
     *
     * @date 2016-1-25 下午1:48:12
     * @author yjh
     */
    public void setIvBackBackground(int resid) {
        iv_back.setBackgroundResource(resid);
    }

    /**
     * 标题栏事件监听
     *
     * @author Administrator
     */
    public interface CommonTitleListener {

        /**
         * 标题栏返回按钮监听
         * 在当前activity销毁前的操作
         *
         * @date 2015-12-15 下午4:52:54
         * @author yjh
         */
        void onBack();
    }

}
